package com.example.shopping.parsers;

import com.example.shopping.api.SHResponseData;
import com.example.shopping.shdatas.SHError;
import com.example.shopping.shdatas.SHProduce;
import com.example.shopping.shdatas.SHSeller;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by doanht on 12/5/13.
 */
public class SHProduceParserData extends SHBaseParserData {
    public String requiredResponseType() {
        return "SearchParser";
    }

    public SHResponseData parseData(JSONObject response) throws org.json.JSONException {
        if (response == null) return null;

        SHError error = null;
        try {
            error = (SHError) this.errorParserData.parseData(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (error != null && (error.statusCode > 200 || error.statusCode <= 0)) {
            return error;
        }

        SHProduce produce = null;
        JSONObject jsonItem = response.getJSONObject("item");
        if (jsonItem != null) {
            produce = new SHProduce();

            produce.itemCd = formatString(jsonItem.getString("item_cd"));
            produce.itemNameKj = formatString(jsonItem.getString("item_name_kj").trim());
            produce.itemNameKn = formatString(jsonItem.getString("item_name_kn").trim());
            produce.dispSpecName = formatString(jsonItem.getString("disp_spec_name").trim());
            produce.makerName = formatString(jsonItem.getString("maker_name").trim());
            produce.sellerItemCd = formatString(jsonItem.getString("seller_item_cd").trim());

            try {
                produce.taxKbn = jsonItem.getInt("tax_kbn");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                produce.itemKbn = jsonItem.getInt("item_kbn");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                produce.orderQty = jsonItem.getInt("order_qty");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            try {
                produce.cost = jsonItem.getDouble("cost");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                produce.price = jsonItem.getDouble("price");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            try {
                produce.regDatetime = convertFromMilliseconds(jsonItem.getLong("reg_datetime"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                produce.updDatetime = convertFromMilliseconds(jsonItem.getLong("upd_datetime"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            JSONObject jsonSeller = null;
            try {
                jsonSeller = jsonItem.getJSONObject("seller");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (jsonSeller != null) {
                SHSeller seller = new SHSeller();
                try {
                    seller.sellerCd = formatString(jsonSeller.getString("seller_cd").trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    seller.sellerName = formatString(jsonSeller.getString("seller_name").trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                try {
                    seller.regDatetime = convertFromMilliseconds(jsonSeller.getLong("reg_datetime"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    seller.updDatetime = convertFromMilliseconds(jsonSeller.getLong("upd_datetime"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                produce.seller = seller;
            }
            produce.updateOldProduce();
        }


        return produce;
    }

    private Date convertFromString(String strDate) {
        Date varDate = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        try {
            varDate = dateFormat.parse(strDate);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return varDate;
    }

    private Date convertFromMilliseconds(long milliseconds) {
        Date varDate = null;
        try {
            varDate = new Date(milliseconds);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return varDate;
    }

    private String formatString(String str) {
        if (str != null)
            return str.replaceAll("　", "");
        return null;
    }
}
